**** Project: Who Accepts Party Policy Change? The Individual-Level Determinants of Policy Change Acceptance
**** Authors: Maurits J. Meijers & Ruth Dassonneville
**** File Info: Data management for YouGov data
**** Date: January 22, 2024

cd "~/replication"

use "data/data_yougov.dta", clear

* responseid
gen responseid_num = _n
gen responseid = string(responseid_num)

**************************
*** Dependent Variable ***
**************************

* ru9 = policy change rejection
* Create var with DK as missing
gen ru9_new = ru9 if ru9!=0

* Create var with DK as mid value
gen ru9_new2 = ru9 
replace ru9_new2 = 4 if ru9 == 0

* reverse vars
gen change_accept = 8-ru9_new2

*dummy for dks
gen change_accept_dk = 0
replace change_accept_dk = 1 if ru9 == 0 

**************************
*** Independent Variables ***
**************************

*** Extremism: left-right  ***
* With DK as middle value
gen left_right2 = var2 + 1 
replace left_right2 = 6 if left_right2 == 100
gen extremism = abs(left_right2 - 6)

*dummy for dks
gen extremism_dk = 0
replace extremism_dk = 1 if var2 == 99

*** Technocracy ***

* With DK as middle value
gen technocracy1_2 = ru4_1
replace technocracy1_2 = 4 if technocracy1_2 == 99 
gen technocracy2_2 = ru4_2 
replace technocracy2_2 = 4 if technocracy2_2 == 99 
gen technocracy3_2 = ru4_3 
replace technocracy3_2 = 4 if technocracy3_2 == 99 
gen technocracy4_2 = ru4_4 
replace technocracy4_2 = 4 if technocracy4_2 == 99 

factor  technocracy1_2 technocracy2_2 technocracy3_2 technocracy4_2, ipf mineigen(1) 
rotate, blanks(.5) orthogonal varimax
predict technocracy_factor	

*dummy for dks
gen technocracy_dk = 0
replace technocracy_dk = 1 if ru4_1 == 99
replace technocracy_dk = 1 if ru4_2 == 99
replace technocracy_dk = 1 if ru4_3 == 99
replace technocracy_dk = 1 if ru4_4 == 99

*** Partisanship / Party Sympathy ***

* Partisanship with DK -> missing
foreach var of varlist var14_1-var14_26  {
    generate `var'_new = `var'
    replace `var'_new = . if `var' == 99
	label variable `var'_new "`: variable label `var''"
}

* Make var that lists which var is highest values
gen which_max = "" 
gen max = 0 
unab xvars : var14_1_new-var14_26_new 

quietly foreach x of local xvars { 
    replace which_max = "`x'" if `x' > max & `x' != .
    replace max = `x' if `x' > max & `x' != .
}
 
gen preferred_party_de = . 
replace preferred_party_de = 1 if which_max == "var14_1_new"
replace preferred_party_de = 2 if which_max == "var14_2_new"
replace preferred_party_de = 3 if which_max == "var14_3_new"
replace preferred_party_de = 4 if which_max == "var14_4_new"
replace preferred_party_de = 5 if which_max == "var14_5_new"
replace preferred_party_de = 6 if which_max == "var14_6_new"

label define preferred_party_delabel 1 "SPD" 2 "CDU/CSU" 3 "Die Grünen" 4 "FDP" 5 "AfD" 6 "Die Linke"
label values preferred_party_de preferred_party_delabel

gen preferred_party_uk = . 
replace preferred_party_uk = 1 if which_max == "var14_16_new"
replace preferred_party_uk = 2 if which_max == "var14_17_new"
replace preferred_party_uk = 3 if which_max == "var14_18_new"
replace preferred_party_uk = 4 if which_max == "var14_19_new"
replace preferred_party_uk = 5 if which_max == "var14_20_new"

label define preferred_party_uklabel 1 "Conservative Party" 2 "Labour" 3 "Liberal Democrats" 4 "SNP" 5 "Green Party", replace
label values preferred_party_uk preferred_party_uklabel

gen preferred_party_es = . 
replace preferred_party_es = 1 if which_max == "var14_7_new"
replace preferred_party_es = 2 if which_max == "var14_8_new"
replace preferred_party_es = 3 if which_max == "var14_9_new"
replace preferred_party_es = 4 if which_max == "var14_10_new"
replace preferred_party_es = 5 if which_max == "var14_11_new"
replace preferred_party_es = 6 if which_max == "var14_12_new"
replace preferred_party_es = 7 if which_max == "var14_13_new"
replace preferred_party_es = 8 if which_max == "var14_14_new"
replace preferred_party_es = 9 if which_max == "var14_15_new"

label define preferred_party_eslabel 1 "PSOE" 2 "PP" 3 "Vox" 4 "Podemos" 5 "Ciudadanos" 6 "ERC" 7 "Más País" 8 "Junts" 9 "EAJ-PNV" , replace
label values preferred_party_es preferred_party_eslabel

gen preferred_party_pl = . 
replace preferred_party_pl = 1 if which_max == "var14_21_new"
replace preferred_party_pl = 2 if which_max == "var14_22_new"
replace preferred_party_pl = 3 if which_max == "var14_23_new"
replace preferred_party_pl = 4 if which_max == "var14_24_new"
replace preferred_party_pl = 5 if which_max == "var14_25_new"
replace preferred_party_pl = 6 if which_max == "var14_26_new"

label define preferred_party_pllabel 1 "PiS" 2 "KO" 3 "Lewica" 4 "PSL" 5 "Konfederacja" 6 "Polska2050", replace
label values preferred_party_pl preferred_party_pllabel

*** Political Interest ***
* Pol int with DK -> mid value (3)
generate pol_interest = var10
replace pol_interest = 3 if var10 == 977
replace pol_interest = 6-pol_interest

*dummy for dks
gen pol_interest_dk = 0
replace pol_interest_dk = 1 if var10 == 977

*** Populist Attitudes ***

* with DK -> mid value (4)
gen pop1_2 = ru2_1
replace pop1_2 = 4 if ru2_1 == 99
gen pop2_2 = ru2_2 
replace pop2_2 = 4 if ru2_2 == 99
gen pop3_2 = ru2_3 
replace pop3_2 = 4 if ru2_3 == 99
gen pop4_2 = ru2_4 
replace pop4_2 = 4 if ru2_4 == 99
gen pop5_2 = ru2_5 
replace pop5_2 = 4 if ru2_5 == 99
gen pop6_2 = ru2_6 
replace pop6_2 = 4 if ru2_6 == 99

factor pop1_2-pop6_2, ipf mineigen(1)
rotate, blanks(.5) orthogonal varimax
predict popatt_factor
	
*dummy for dks
gen popatt_dk = 0
replace popatt_dk = 1 if ru2_1 == 99
replace popatt_dk = 1 if ru2_2 == 99
replace popatt_dk = 1 if ru2_3 == 99
replace popatt_dk = 1 if ru2_4 == 99
replace popatt_dk = 1 if ru2_5 == 99
replace popatt_dk = 1 if ru2_6 == 99

**************************
*** Labelling Variables ***
**************************
label var change_accept "Policy Change Acceptance"
label var extremism "Extremism (Left-Right)"
label var pol_interest "Political Interest"
label var popatt_factor "Populist Attitudes"
label var left_right "Left-Right Self-Placement"
label var technocracy_factor "Technocratic Attitudes"
label var age "Age"
label var income_q_combined "Income Quartile"
label var education_levels_comb "Education"
label define educationlabel 1 "Low Education" 2 "Medium Education" 3 "High Education"
label values education_levels_comb educationlabel
label define incomelabel 1 "1st Income Q" 2 "2nd Income Q" 3 "3rd Income Q" 4 "4th Income Q"
label values income_q_combined incomelabel
label define countrylabel 1 "Germany" 2 "United Kingdom" 3 "Spain" 4 "Poland" 5 "The Netherlands", replace
label values country countrylabel
label variable gender "Gender"
label variable pop1_2 "POP1"
label variable pop2_2 "POP2"
label variable pop3_2 "POP3"
label variable pop4_2 "POP4"
label variable pop5_2 "POP5"
label variable pop6_2 "POP6"

* left/right extremism measure	   
gen extremism_left = .
replace extremism_left = 0 if left_right2>6
replace extremism_left = 1 if left_right2 == 5
replace extremism_left = 2 if left_right2 == 4
replace extremism_left = 3 if left_right2 == 3
replace extremism_left = 4 if left_right2 == 2
replace extremism_left = 5 if left_right2 == 1

gen extremism_right = .
replace extremism_right = 0 if left_right2<6
replace extremism_right = 1 if left_right2 == 7
replace extremism_right = 2 if left_right2 == 8
replace extremism_right = 3 if left_right2 == 9
replace extremism_right = 4 if left_right2 == 10
replace extremism_right = 5 if left_right2 == 11

label var extremism_left "Left-Wing Extremism"
label var extremism_right "Right-Wing Extremism"

**************************
*** Merge ***
**************************

merge 1:1 responseid using "/Users/maurits.meijers/Dropbox/academic/research/Veni_Projects/Projects/PolicyChange_Acceptance/replication/data/data_nl.dta"

* Redo factor vars: technocracy and populism 
drop technocracy_factor
factor  technocracy1_2 technocracy2_2 technocracy3_2 technocracy4_2, ipf mineigen(1) 
rotate, blanks(.5) orthogonal varimax
predict technocracy_factor	
label var technocracy_factor "Technocratic Attitudes"

*Technocratic attitudes *
rename technocracy1_2 tec3
rename technocracy2_2 tec4
rename technocracy3_2 tec1
rename technocracy4_2 tec2

drop popatt_factor
factor pop1_2-pop6_2, ipf mineigen(1)
rotate, blanks(.5) orthogonal varimax
predict popatt_factor
label var popatt_factor "Populist Attitudes"

* Differently ordered country var for multidensity plot
gen country2 = .
replace country2 = 1 if country==1
replace country2 = 2 if country==4
replace country2 = 3 if country==3
replace country2 = 4 if country==5
replace country2 = 5 if country==2

label define countrylabel2 1 "Germany" 5 "United Kingdom" 3 "Spain" 2 "Poland" 4 "The Netherlands", replace
label values country2 countrylabel2

* drop all other vars 
keep  change_accept  pol_interest technocracy_factor extremism popatt_factor gender age  education_levels_comb country country2 preferred_party_de preferred_party_uk preferred_party_es preferred_party_pl preferred_party_nl tec1 tec2 tec3 tec4       pop1_2 pop2_2 pop3_2 pop4_2 pop5_2 pop6_2 change_accept_dk pol_interest_dk technocracy_dk extremism_dk popatt_dk weight extremism_right extremism_left responseid

save "main_data.dta", replace
